#include "Dictionary.h"

Dictionary::Dictionary(const string datapath)
:_datapath(datapath)
{
    initEn();
    initCn();
}

void Dictionary::initEn(){
    //dict
    ifstream ifs_dic(_datapath + string("/en_dict.txt"));
    if(!ifs_dic.good()){
        cout << "dictpath error!\n";
        exit(-1);
    }

    string line;
    while(getline(ifs_dic, line)){
        string word;
        size_t s_freq;
        istringstream iss(line);
        iss >> word;
        iss >> s_freq;
        _en_dict.push_back({word, s_freq});
    }
    //index
    ifstream ifs_index(_datapath + string("/en_index.txt"));
    if(!ifs_index.good()){
        cout << "dictpath error!\n";
        exit(-1);
    }

    while(getline(ifs_index, line)){
        string word;
        size_t s_idx;
        istringstream iss(line);
        iss >> word;
        while(iss >> s_idx){
            _en_index[word].insert(s_idx);
        }
    }
}

void Dictionary::initCn(){
    //dict
    ifstream ifs_dic(_datapath + string("/cn_dict.txt"));
    if(!ifs_dic.good()){
        cout << "dictpath error!\n";
        exit(-1);
    }

    string line;
    while(getline(ifs_dic, line)){
        string word;
        size_t s_freq;
        istringstream iss(line);

        iss >> word;
        iss >> s_freq;

        _cn_dict.push_back({word, s_freq});
    }
    //index
    ifstream ifs_index(_datapath + string("/cn_index.txt"));
    if(!ifs_index.good()){
        cout << "dictpath error!\n";
        exit(-1);
    }

    while(getline(ifs_index, line)){
        string word;
        size_t s_idx;
        istringstream iss(line);
        iss >> word;
        while(iss >> s_idx){
            _cn_index[word].insert(s_idx);
        }
    }
}

vector<pair<string, size_t>>& Dictionary::getEnDict(){
    return _en_dict;
}

map<string, set<int>>& Dictionary::getEnIndex(){
    return _en_index;
}

vector<pair<string, size_t>>& Dictionary::getCnDict(){
    return _cn_dict;
}

map<string, set<int>>& Dictionary::getCnIndex(){
    return _cn_index;
}
